<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>	
<%@ include file="/pub/headmeta.jsp" %>
<%request.setAttribute("classId", request.getParameter("classId")); %>
</head>
<body class="edit-bgr-color">
	<div class="right-head"><span class="right-head-font">新增宝宝营养</span></div>
	<div align="center" style="width: 98%">	
		<form id="addForm" method="post" enctype="multipart/form-data">			
			<table cellpadding="5" style="width: 85%; font-size: 12px;">
				<tr>
					<td width="35%" align="right">商品名称：</td>
					<td width="65%" align="left"><input type="text" name="goodsName" id="goodsName" style="width: 80%" />&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>
				<tr>
					<td align="right">商品图片：</td>
					<td align="left"><input type="text" name="goodsPic" id="goodsPic" style="width: 80%" />&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>
				<tr>
					<td align="right">原始链接：</td>
					<td align="left"><textarea name="link" id="link"  rows="3" style="width: 80%"></textarea>&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>
				<tr>
					<td align="right">年龄段：</td>
					<td align="left"><select name="ageId" id="ageId" style="width: 80%"><option value="">-请选择-</option></select>&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>
				<tr>
					<td align="right">所属分类：</td>
					<td align="left"><select name="classId" id="classId" style="width: 80%"><option value="">-请选择-</option></select>&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>
				<tr>
					<td align="right">推荐理由：</td>
					<td align="left"><textarea name="recommend" id="recommend"  rows="8" style="width: 80%"></textarea>&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>
			</table>
			<div class="edit-line"></div>
			<input  type="button" class="button-1" value="新增" onclick="doAdd();"/>
			<input  type="button" class="button-1" value="重置" onclick="resetEx(this.form);"/>						
			<input  type="button" class="button-1" value="返回" onclick="back();"/>
		</form>
	</div>
</body>
</html>
<script type="text/javascript">
var types;
<!--
//自动调用初始化
$(document).ready(function(){	
	//填充年龄段
	$.ajax({type : "post", url : "${ctx}/age!getList.action?page=1&pageSize=100", data : '', async : false, success : function callback(txt){   
		var json = $.parseJSON(txt);			
		if (json.code==0) {		
			var objs=json.info;
			for(var i=0;i<objs.length;i++){
				$("#ageId").append("<option value='"+objs[i].ageId+"'>"+objs[i].ageArea+"</option>"); 
			}
		}else{
			alert('查询失败！');
		}
	}});
	
	//填充分类
	$.ajax({type : "post", url : "${ctx}/classify!getSelectType.action?id=${classId}", data : '', async : false, success : function callback(txt){   
		var json = $.parseJSON(txt);	
		if (json.code==0) {		
			types=json.info;
			for(var i=0;i<types.length;i++){
				$("#classId").append("<option value='"+types[i].classId+"'>"+types[i].className+"</option>"); 
			}
		}else{
			alert('查询失败！');
		}
	}});

});

//新增
function doAdd() {
	if ($("#goodsName").val()==""){
		alert('商品名称不能为空！');
		return;
	}
	if ($("#goodsPic").val()==""){
		alert('商品图片不能为空！');
		return;
	}
	if ($("#link").val()==""){
		alert('原始链接不能为空！');
		return;
	}
	if ($("#ageId").val()==""){
		alert('年龄段不能为空！');
		return;
	}
	if ($("#classId").val()==""){
		alert('所属分类不能为空！');
		return;
	}
	for(var i=0;i<types.length;i++){
		if(types[i].classId==$("#classId").val()){
			if(types[i].className.indexOf("——")!=-1){
				alert('所属分类请选择三级分类！');
				return;
			}
		}
	}	
	if ($("#recommend").val()==""){
		alert('推荐理由不能为空！');
		return;
	}	
	$.uploadEx({        
		url: '${ctx}/goods!add.action', 
		formId: "addForm",
		dataType: 'json',  
		onSend: function() {
	        return true;
	    },     
		onComplate: function(json) {        	
			if (json.code==0) {	
				alert('新增成功！');	
				$("#goodsName").val("");
				$("#goodsPic").val("");
				$("#link").val("");
				$("#recommend").val("");
			}else{
				alert('新增失败！');
			}
		}
	});
	
}
//返回
function back(){
	location.href="${ctx}/goods!viewEditList.action?classId=${classId}";
}
//-->
</script>
